// pages/gjIndex/comment/comment.js
import config from "../../../config.js";
const openIdUrl = config;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    repShow: false,
    list: [],
    content: '',
    commentId: 0
  },

  rep: function(e) {
    console.log(e)
    this.setData({
      commentId: e.currentTarget.dataset.comid
    })
    this.setData({
      repShow: true
    })
  },

  closeRep: function() {
    this.setData({
      repShow: false,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.loadData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  loadData: function() {
    var that = this;
    wx.showLoading({
      title: '加载中...',
    })
    wx.request({
      url: openIdUrl.commentList,
      data: {
        start: 0,
        size: 10,
        search: ''
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': wx.getStorageSync("session")
      },
      success: function(res) {
        console.log(res);
        wx.hideLoading();
        if (res.data.status == 200) {
          that.setData({
            list: res.data.data
          })

        } else {
          wx.hideLoading();
          wx.showToast({
            title: res.data.message + "",
            icon: 'none',
            mask: true,
            duration: 2000
          })
        }

      },
      error: function() {
        wx.showToast({
          title: "请求失败,请联系管理员",
          icon: 'none',
          mask: true,
          duration: 2000
        })
      }
    })

  },
  contentChange: function(e) {
    console.log(e.detail.value)
    this.setData({
      content: e.detail.value
    })

  },
  replyAction: function(e) {
    var that = this;
    wx.showLoading({
      title: '加载中...',
    })
    wx.request({
      url: openIdUrl.replyUserComment,
      data: {
        commentId: this.data.commentId,
        content: this.data.content
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': wx.getStorageSync("session")
      },
      success: function(res) {
        console.log(res);
        wx.hideLoading();
        if (res.data.status == 200) {
          that.loadData();
          that.setData({
            repShow: false
          })
        }

        wx.hideLoading();
        wx.showToast({
          title: res.data.message + "",
          icon: 'none',
          mask: true,
          duration: 2000
        })

      },
      error: function() {
        wx.showToast({
          title: "请求失败,请联系管理员",
          icon: 'none',
          mask: true,
          duration: 2000
        })
      }
    })
  },
  hideReply: function() {
    this.setData({
      repShow: false
    })
  }
})